package com.chenjunlin.shortlink.admin.controller;

import cn.hutool.db.PageResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chenjunlin.shortlink.admin.common.convention.result.Result;
import com.chenjunlin.shortlink.admin.common.convention.result.Results;
import com.chenjunlin.shortlink.admin.dao.entity.GroupDO;
import com.chenjunlin.shortlink.admin.dto.reqtDTO.GroupAddReqDTO;
import com.chenjunlin.shortlink.admin.dto.reqtDTO.GroupPageReqDTO;
import com.chenjunlin.shortlink.admin.dto.reqtDTO.GroupUpdateReqDTO;
import com.chenjunlin.shortlink.admin.service.GroupService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/short-link/admin/v1/group")
@RequiredArgsConstructor
public class GroupController {
    private final GroupService groupService;

    /**
     * 新增分组
     * @param groupAddReqDTO
     * @return
     */
    @PostMapping("")
    public Result<Void> addGroup(@RequestBody GroupAddReqDTO groupAddReqDTO){
        groupService.add(groupAddReqDTO);
        return Results.success();
    }

    /**
     * 分组分页查询
     */
    @GetMapping("")
    public Result<IPage<GroupDO>> pageList(GroupPageReqDTO pageReqDTO){
        return Results.success(groupService.pageResult(pageReqDTO));
    }

    /**
     * 分组修改功能
     */
    @PutMapping("")
    public Result<Void> updateGroup (@RequestBody GroupUpdateReqDTO groupUpdateReqDTO){
        groupService.updateByDTO(groupUpdateReqDTO);
        return Results.success();
    }

    /**
     * 根据gid删除分组
     * @param gid
     * @return
     */
    @DeleteMapping("")
    public Result<Void> deleteGroupByGid(String gid){
        groupService.deleteByGid(gid);
        return Results.success();
    }
}
